// 引入接口
var express = require('express');
const { get, add, getOne, update, delImg, deleteBtn, reviseBtn, delPoster,find,getHot,getAll,findInfo} = require("../api/movies");
// 路由
var router = express.Router();

// 和页面相关的操作
router.get('/', async function (req, res, next) {
    // 解构当前页和每页显示条数
    const { curPage, eachPage } = req.query;
    const data = await get({ curPage, eachPage });
    res.send(data);
});

// 添加电影 接收页面post请求 转发请求到api下获取参数
router.post('/add-movies', async function (req, res, next) {
    // // 解构当前显示数据 create创建添加内容
    const { cname, ename, type, area, poster, time, upDate, score, count, intro, isClassic, director, actor } = req.body;
    const data = await add({ cname, ename, type, area, poster, time, upDate, score, count, intro, isClassic, director, actor })
    res.send(data);
});

// 上传文件
router.patch('/:_id', async function (req, res, next) {
    const { _id } = req.params;
    const data = req.body;
    await update({ _id }, data);
    const result = await getOne(_id); //通过getOne拿到最新的数据
    res.send(result);
});


// 图片删除
router.delete('/:_id/movies/:url', async function (req, res, next) {
    const { _id, url } = req.params;
    await delImg({ _id, url }); //删除完成后台数据
    const data = await getOne(_id);  //更新后台数据
    res.send(data);
});

// 删除海报
router.delete('/:_id/moviesPoster/:url', async function (req, res, next) {
    const { _id, url } = req.params;
    await delPoster({ _id, url }); //删除完成后台数据
    const data = await getOne(_id);  //更新后台数据
    res.send(data);
});

//点击删除
router.delete('/:_id', async function (req, res, next) {
    // 解析路径拿到电影id
    const { _id } = req.params;
    const movies = await deleteBtn(_id);
    res.send(movies);
});

// 修改电影信息
router.patch('/:_id', async function (req, res, next) {
    const { _id } = req.params;
    const { cname, ename, type, area, time, upDate, score, count, intro, isClassic, director, actor}=req.body;
    const data = await reviseBtn({ cname, ename, type, area, time, upDate, score, count, intro, isClassic, director, actor});
    res.send(data);
});

// 获取需要渲染的电影信息
router.get('/getone/:_id', async function (req, res, next) {
    // req.params从传入参数中获取数据
    const { _id } = req.params;
    console.log(_id);
    const data = await getOne(_id);// 通过_id找到对应的电影数据id
    res.send(data); // 将获取的数据发送到页面
})

// 获取查询的电影
router.get('/find', async function (req, res, next) {
    // req.params从传入参数中获取数据
    const { isClassic,type} = req.query;
    // 通过cinemasId找到对应放映厅
    const data = await find({isClassic,type});
    // 将获取的放映厅数据发送到页面
    res.send(data);
  });
  
// 获取热门电影
router.get('/hot', async function (req, res, next) {
    const data = await getHot();
    console.log(data);
    res.send(data);
});

// 获取所有电影
router.get('/getAll', async function (req, res, next) {
    const data = await getAll();
    res.send(data);
});

// 模糊查询影院名
router.get('/findInfo', async function (req, res, next) {
    // 获取传输的字符串
    const {str} = req.query;
    // 通过str找到对应放映厅
    const data = await findInfo(str);
    // 将获取的放映厅数据发送到页面
    res.send(data);
  });

// 暴露接口
module.exports = router;